package com.example.demoapp.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;

import com.example.demoapp.R;
import com.example.demoapp.activity.LoginActivity;
import com.example.demoapp.activity.MyAgreeActivity;
import com.example.demoapp.activity.MyCollectionActivity;
import com.example.demoapp.activity.MyHistoryActivity;
import com.example.demoapp.activity.PersonInfoActivity;
import com.example.demoapp.activity.settingInfoActivity;
import com.example.demoapp.bean.User;
import com.google.gson.Gson;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PersonFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PersonFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    //登录按钮+用户头像

    private CircleImageView user_avatar;

    private ImageButton setting;
    private LinearLayout info;
    private TextView user_name;
    private LinearLayout mycollect;
    private LinearLayout history;
    private LinearLayout myagree;
    // 信息传递
    private SharedPreferences preferences;
    private View view;
    private SearchView searchview;
    public PersonFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment NewsFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static PersonFragment newInstance(String param1, String param2) {
        PersonFragment fragment = new PersonFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        changeLoginInfo();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_person,container,false);
        //--------------------初始化视图--------------------//
        //登录按钮+用户头像
        user_avatar = view.findViewById(R.id.user_avatar);
        info = view.findViewById(R.id.info);
        // 刷新用户信息
        user_name=view.findViewById(R.id.user_name);
        mycollect = view.findViewById(R.id.mycollect);
        history = view.findViewById(R.id.history);
        myagree = view.findViewById(R.id.myagree);
        // 获取用户信息
        changeLoginInfo();

        mycollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), MyCollectionActivity.class));
            }
        });
        history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), MyHistoryActivity.class));
            }
        });
        myagree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startActivity(new Intent(getContext(), MyAgreeActivity.class));
            }
        });
        return view;
    }

    private void changeLoginInfo(){
        preferences = getContext().getSharedPreferences("LoginPrefs", Context.MODE_PRIVATE);
        if(preferences.contains("username")){
            User user = new Gson().fromJson(preferences.getString("user",""),User.class);
            user_avatar.setImageResource(R.drawable.avatar);
            user_name.setText(user.getNickName());
            info.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    跳出个人信息页面
                    Intent info = new Intent();
                    info.setClass(getContext(), PersonInfoActivity.class);
                    startActivity(info);
                }
            });
            user_avatar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    查看头像
                }
            });
        }else {
            user_avatar.setImageResource(R.drawable.circle_btn_press);
            //设置点击事件
            user_avatar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent login = new Intent();
                    login.setClass(getContext(), LoginActivity.class);
                    startActivity(login);
                }
            });
            info.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    查看头像
                }
            });

            user_name.setText("请登录");
            Log.i("login","无登录信息");
        }

        setting= (ImageButton) view.findViewById(R.id.setting);
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent setinfo = new Intent();
                setinfo.setClass(getContext(), settingInfoActivity.class);
                startActivity(setinfo);
            }
        });


    }
}